package mia.beans;

import java.util.Date;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.hibernate.Session;

import mia.dao.AdvertisementTypeEntityDAO;
import mia.dao.CategoryEntityDAO;
import mia.dao.UserEntityDAO;
import mia.entity.AdvertisementEntity;
import mia.entity.AdvertisementTypeEntity;
import mia.entity.AttachmentEntity;
import mia.entity.CategoryEntity;
import mia.entity.UserEntity;
import mia.util.HibernateManager;

@ManagedBean(name = "advertisementRegBean")
@RequestScoped
public class AdvertisementRegBean {

	@ManagedProperty("#{userLoginBean.user}")
	private UserEntity user;

	private AdvertisementEntity advertisement = new AdvertisementEntity();

	private Map<String, AdvertisementTypeEntity> types = null;
	private String selectedType;

	private Map<String, CategoryEntity> categories = null;
	private String selectedCategory;

	private String attachment1;
	private String attachment2;
	private String attachment3;

	private String regAdvertisementStatus;

	public AdvertisementEntity getAdvertisement() {
		return advertisement;
	}

	public void setAdvertisement(AdvertisementEntity advertisement) {
		this.advertisement = advertisement;
	}

	public Map<String, AdvertisementTypeEntity> getTypes() {
		if (types == null)
			types = AdvertisementTypeEntityDAO.getAdvertisementTypes();
		return types;
	}

	public void setTypes(Map<String, AdvertisementTypeEntity> types) {
		this.types = types;
	}

	public Map<String, CategoryEntity> getCategories() {
		if (categories == null)
			categories = CategoryEntityDAO.getCategories();
		return categories;
	}

	public void setCategories(Map<String, CategoryEntity> categories) {
		this.categories = categories;
	}

	public String getSelectedType() {
		return selectedType;
	}

	public void setSelectedType(String selectedType) {
		this.selectedType = selectedType;
	}

	public String getSelectedCategory() {
		return selectedCategory;
	}

	public void setSelectedCategory(String selectedCategory) {
		this.selectedCategory = selectedCategory;
	}

	public String getAttachment1() {
		return attachment1;
	}

	public void setAttachment1(String attachment1) {
		this.attachment1 = attachment1;
	}

	public String getAttachment2() {
		return attachment2;
	}

	public void setAttachment2(String attachment2) {
		this.attachment2 = attachment2;
	}

	public String getAttachment3() {
		return attachment3;
	}

	public void setAttachment3(String attachment3) {
		this.attachment3 = attachment3;
	}

	public UserEntity getUser() {
		return user;
	}

	public void setUser(UserEntity user) {
		this.user = user;
	}

	public String getRegAdvertisementStatus() {
		return regAdvertisementStatus;
	}

	public void setRegAdvertisementStatus(String regAdvertisementStatus) {
		this.regAdvertisementStatus = regAdvertisementStatus;
	}

	public String trySave() {

		FacesContext context = FacesContext.getCurrentInstance();
		Date now = new Date();


			if (advertisement.getExpireDate() != null
					&& (advertisement.getExpireDate().before(
							advertisement.getStartDate()) || advertisement
							.getExpireDate().equals(
									advertisement.getStartDate()))) {
				context.addMessage(
						"change",
						new FacesMessage(
								FacesMessage.SEVERITY_ERROR,
								"Data wygaśnięcia ogłoszenia nie może być wcześniejsza lub równa dacie jego rozpoczęcia!",
								""));
				return null;
			} else {

				Session session = HibernateManager.getSession();
				session.beginTransaction();
				UserEntity u = UserEntityDAO.getUserByLogin(session,
						user.getLogin());

				System.out.println("Kategoria" + getCategories().size());
				advertisement
						.setCategory(getCategories().get(selectedCategory));
				advertisement.setType(getTypes().get(selectedType));
				advertisement.setUser(u);

				try {
					session.save(advertisement);
					for (AttachmentEntity at : advertisement.getAttachments())
						session.save(at);
					session.getTransaction().commit();
					context.addMessage("adding", new FacesMessage(
							FacesMessage.SEVERITY_INFO, "Dodano ogłoszenie", ""));

					return "myAdvertisements";

				} catch (Exception ex) {
					session.getTransaction().rollback();
					context.addMessage(
							"change",
							new FacesMessage(
									FacesMessage.SEVERITY_ERROR,
									"Wystąpił błąd, ogłoszenie nie zostało dodane!",
									""));
					return null;
				} finally {
					session.close();
				}
			
		}
	}
}
